home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / GameKit / gamekit-1 / GameInfo.m < prev    next >
Text File  |  1995-06-12  |  9KB  |  298 lines

  1. // GameInfo.m -- This object allows you to set global parameters that
  2. //         affect the GameKit in various ways.
  3.  
  4. #import <gamekit/gamekit.h>
  5. #import <daymisckit/daymisckit.h>
  6. #import <appkit/appkit.h>
  7.  
  8. @implementation GameInfo
  9.  
  10. - init    // implement to generate an empty slot for server
  11. {
  12.     int i;
  13.     [super init];
  14.     maxHighScores = 10; numHighScoreTables = 1; numSoundStreams = 1;
  15.     numSounds = GK_DEFNUMSOUNDS; numSoundTypes = GK_DEFNUMTYPES;
  16.     numOneUps = 3;
  17.     // this object assumes that this array is allocated.  Don't use the
  18.     // object if it isn't or you'll get memory access exceptions (probably
  19.     // on address 0x0)
  20.     maxHighScoresPerPlayer = (int *)malloc(sizeof(int) *
  21.             2 * numHighScoreTables);
  22.     for (i=0; i<(numHighScoreTables * 2); i++) maxHighScoresPerPlayer[i] = 0;
  23.     slotType = [[DAYString alloc] initString:"HighScoreSlot"];
  24.     encryptedPassword = [[DAYString alloc] initString:"GK72ol87WD.CE"];
  25.         // the above password is "NONE".  Override with your own.
  26.     return self;
  27. }
  28.  
  29. - free
  30. {    // free all the sub-objects.
  31.     [slotType free];
  32.     [encryptedPassword free];
  33.     if (maxHighScoresPerPlayer) free(maxHighScoresPerPlayer);
  34.     return self;
  35. }
  36.  
  37. - dumpToLog:aLogFile    // used by server when logging
  38. {    // aLogFile must be a kind of the DAYLogFile class
  39.     char *string = (char *)malloc(256); int i;
  40.     id tempString = [[DAYString alloc] init];
  41.     sprintf(string, "    numSounds = %d\n    numSoundTypes = %d\n    numSoundStreams = %d\n    maxHighScores = %d\n    numHighScoreTables = %d\n    numOneUps = %d\n    slotType = \"%s\"\n    encryptedPassword = \"%s\"\n",
  42.             numSounds, numSoundTypes, numSoundStreams,
  43.             maxHighScores, numHighScoreTables, numOneUps,
  44.             [slotType stringValue], [encryptedPassword stringValue]);
  45.     [tempString setStringValue:string];
  46.     [aLogFile addLineToLogFile:tempString];
  47.     for (i=0; i<numHighScoreTables; i++) {
  48.         sprintf(string, "        maxHighScoresPerPlayer for table %d (net = NO) = %d\n        maxHighScoresPerPlayer for table %d (net = YES) = %d\n",
  49.                 i, maxHighScoresPerPlayer[i*2],
  50.                 i, maxHighScoresPerPlayer[i*2+1]);
  51.         [tempString setStringValue:string];
  52.         [aLogFile addLineToLogFile:tempString];
  53.     }
  54.     free(string);
  55.     [tempString free];
  56.     return self;
  57. }
  58.  
  59. // The following methods access stored info.
  60. - slotType            // For the high score system
  61. {
  62.     return slotType;
  63. }
  64.  
  65. - (int)numOneUps                // for the high score system
  66. {
  67.     return numOneUps;
  68. }
  69.  
  70. - (int)maxHighScores                // for the high score system
  71. {
  72.     return maxHighScores;
  73. }
  74.  
  75. - (int)numHighScoreTables    // for the high score system
  76. {
  77.     return numHighScoreTables;
  78. }
  79.  
  80. - encryptedPassword    // For network high score table access
  81. {
  82.     return encryptedPassword;
  83. }
  84.  
  85. - (int)numSounds            // for the sound system
  86. {
  87.     return numSounds;
  88. }
  89.  
  90. - (int)numSoundTypes        // for the sound system
  91. {
  92.     return numSoundTypes;
  93. }
  94.  
  95. - (int)numSoundStreams        // for the sound system
  96. {
  97.     return numSoundStreams;
  98. }
  99.  
  100.  
  101. // The following methods change stored info.  All return self.
  102. - setnumSoundTypes:(int)anInt
  103. {
  104.     numSoundTypes = anInt;
  105.     return self;
  106. }
  107.  
  108. - setnumSoundStreams:(int)anInt
  109. {
  110.     numSoundStreams = anInt;
  111.     return self;
  112. }
  113.  
  114. - setnumSounds:(int)anInt
  115. {
  116.     numSounds = anInt;
  117.     return self;
  118. }
  119.  
  120. - setnumHighScoreTables:(int)anInt
  121. { // copy over the max scores per player and reallocate it to the right size
  122.     int i, *newMaxScores = (int *)malloc(sizeof(int) * 2 * anInt);
  123.     for (i=0; i<anInt; i++) { // return zero if out of range...
  124.         // if you compile with NOISYDEBUG, you'll see a mass of "errors"
  125.         // every time this array is enlarged.  Ignore them; it's on purpose!
  126.         newMaxScores[i*2] = [self maxScoresPerPlayerTable:i net:0];
  127.         newMaxScores[i*2+1] = [self maxScoresPerPlayerTable:i net:1];
  128.     }
  129.     free(maxHighScoresPerPlayer);
  130.     maxHighScoresPerPlayer = newMaxScores;
  131.     numHighScoreTables = anInt;
  132.     return self;
  133. }
  134.  
  135. - setMaxHighScores:(int)anInt
  136. {
  137.     maxHighScores = anInt;
  138.     return self;
  139. }
  140.  
  141. - setSlotType:(const char *)aString
  142. {
  143.     [slotType setStringValue:aString];
  144.     return self;
  145. }
  146.  
  147. - setEncryptedPassword:(const char *)aString
  148. {
  149.     [encryptedPassword setStringValue:aString];
  150.     return self;
  151. }
  152.  
  153. - setNumOneUps:(int)oneUps
  154. {
  155.     numOneUps = oneUps;
  156.     return self;
  157. }
  158.  
  159. - (int)maxScoresPerPlayerTable:(int)theTable net:(BOOL)which
  160. {
  161.     if ((theTable >= numHighScoreTables) || (theTable < 0)) {
  162. #ifdef NOISYDEBUG
  163.         fprintf(stderr,
  164.             "GameInfo:  Table arg (%d) to -maxScoresPerPlayerTable:net: out of range.\n",
  165.             theTable);
  166. #endif
  167.         return 0;  // out of range
  168.     }
  169.     return maxHighScoresPerPlayer[theTable * 2 + (which ? 1 : 0)];
  170. }
  171.  
  172. - setMaxScoresPerPlayerTable:(int)theTable net:(BOOL)which to:(int)val
  173. {
  174.     if ((theTable >= numHighScoreTables) || (theTable < 0)) {
  175. #ifdef NOISYDEBUG
  176.         fprintf(stderr,
  177.             "GameInfo:  Table arg (%d) to -setMaxScoresPerPlayerTable:net:to: out of range.\n",
  178.             theTable);
  179. #endif
  180.         return nil;  // out of range
  181.     }
  182.     maxHighScoresPerPlayer[theTable * 2 + (which ? 1 : 0)] = val;
  183.     return self;
  184. }
  185.  
  186. // for archiving to/from a file
  187. - read:(NXTypedStream *)stream
  188. {
  189.     int i;
  190.     [super read:stream];
  191.     NXReadTypes(stream, "iiiiii", &maxHighScores, &numHighScoreTables,
  192.             &numSounds, &numSoundTypes, &numSoundStreams, &numOneUps);
  193.     slotType = NXReadObject(stream);
  194.     encryptedPassword = NXReadObject(stream);
  195.     if (maxHighScoresPerPlayer)    free(maxHighScoresPerPlayer);
  196.     maxHighScoresPerPlayer = (int *)malloc(sizeof(int) *
  197.         2 * numHighScoreTables);
  198.     for (i=0; i<(numHighScoreTables * 2); i++)
  199.         NXReadType(stream, "i", &maxHighScoresPerPlayer[i]);
  200. #ifdef NOISYDEBUG
  201.     fprintf(stderr,
  202.             "GameInfo:  -read:\n    maxHighScores = %d\n", maxHighScores);
  203.     fprintf(stderr, "    numHighScoreTables = %d\n", numHighScoreTables);
  204.     fprintf(stderr, "    numSounds = %d\n", numSounds);
  205.     fprintf(stderr, "    numSoundTypes = %d\n", numSoundTypes);
  206.     fprintf(stderr, "    numSoundStreams = %d\n", numSoundStreams);
  207.     fprintf(stderr, "    numOneUps = %d\n", numOneUps);
  208.     fprintf(stderr, "    slotType = %s\n", [slotType stringValue]);
  209.     fprintf(stderr, "    encryptedPassword = %s\n",
  210.             [encryptedPassword stringValue]);
  211.     for (i=0; i<(numHighScoreTables * 2); i++)
  212.         fprintf(stderr, "    maxHighScoresPerPlayer[%d] = %d\n",
  213.                 i, maxHighScoresPerPlayer[i]);
  214.     fprintf(stderr, "\n");
  215. #endif
  216.     return self;
  217. }
  218.  
  219. - write:(NXTypedStream *)stream
  220. {
  221.     int i;
  222.     [super write:stream];
  223. #ifdef NOISYDEBUG
  224.     fprintf(stderr,
  225.             "GameInfo:  -write:\n    maxHighScores = %d\n", maxHighScores);
  226.     fprintf(stderr, "    numHighScoreTables = %d\n", numHighScoreTables);
  227.     fprintf(stderr, "    numSounds = %d\n", numSounds);
  228.     fprintf(stderr, "    numSoundTypes = %d\n", numSoundTypes);
  229.     fprintf(stderr, "    numSoundStreams = %d\n", numSoundStreams);
  230.     fprintf(stderr, "    numOneUps = %d\n", numOneUps);
  231.     fprintf(stderr, "    slotType = %s\n", [slotType stringValue]);
  232.     fprintf(stderr, "    encryptedPassword = %s\n",
  233.             [encryptedPassword stringValue]);
  234.     for (i=0; i<(numHighScoreTables * 2); i++)
  235.         fprintf(stderr, "    maxHighScoresPerPlayer[%d] = %d\n",
  236.                 i, maxHighScoresPerPlayer[i]);
  237.     fprintf(stderr, "\n");
  238. #endif
  239.     NXWriteTypes(stream, "iiiiii", &maxHighScores, &numHighScoreTables,
  240.             &numSounds, &numSoundTypes, &numSoundStreams, &numOneUps);
  241.     NXWriteObject(stream, slotType);
  242.     NXWriteObject(stream, encryptedPassword);
  243.     for (i=0; i<(numHighScoreTables * 2); i++)
  244.         NXWriteType(stream, "i", &maxHighScoresPerPlayer[i]);
  245.     return self;
  246. }
  247.  
  248. // NXTransport protocol implementation:
  249. - encodeUsing:(id <NXEncoding>)portal
  250. {
  251.     int i;
  252.     [portal encodeObjectBycopy:slotType];
  253.     [portal encodeObjectBycopy:encryptedPassword];
  254.     [portal encodeData:&maxHighScores ofType:"i"];
  255.     [portal encodeData:&numHighScoreTables ofType:"i"];
  256.     [portal encodeData:&numSounds ofType:"i"];
  257.     [portal encodeData:&numSoundTypes ofType:"i"];
  258.     [portal encodeData:&numSoundStreams ofType:"i"];
  259.     [portal encodeData:&numOneUps ofType:"i"];
  260.     for (i=0; i<(numHighScoreTables*2); i++)
  261.         [portal encodeData:&maxHighScoresPerPlayer[i] ofType:"i"];
  262.     return self;
  263. }
  264.  
  265. - decodeUsing:(id <NXDecoding>)portal
  266. {
  267.     int i;
  268.     slotType = [portal decodeObject];
  269.     encryptedPassword = [portal decodeObject];
  270.     [portal decodeData:&maxHighScores ofType:"i"];
  271.     [portal decodeData:&numHighScoreTables ofType:"i"];
  272.     [portal decodeData:&numSounds ofType:"i"];
  273.     [portal decodeData:&numSoundTypes ofType:"i"];
  274.     [portal decodeData:&numSoundStreams ofType:"i"];
  275.     [portal decodeData:&numOneUps ofType:"i"];
  276.     if (maxHighScoresPerPlayer) free(maxHighScoresPerPlayer);
  277.     maxHighScoresPerPlayer = (int *)malloc(sizeof(int) *
  278.         2 * numHighScoreTables);
  279.     for (i=0; i<(numHighScoreTables*2); i++)
  280.         [portal decodeData:&maxHighScoresPerPlayer[i] ofType:"i"];
  281.     return self;
  282. }
  283.  
  284. - encodeRemotelyFor:(NXConnection *)connection
  285.     freeAfterEncoding:(BOOL *)flagp isBycopy:(BOOL)isByCopy
  286. {
  287.     if (isByCopy) return self; //encode object (copy it)
  288.     // super will encode the proxy otherwise
  289.     return [super encodeRemotelyFor:connection
  290.                 freeAfterEncoding:flagp isBycopy:isByCopy];
  291. }
  292.  
  293. // Interface Builder support
  294. - (const char *)getInspectorClassName { return "GameInfoInspector"; }
  295. - (NXImage *)getIBImage { return [NXImage findImageNamed:"GameInfoObj"]; }
  296.  
  297. @end
  298.